package com.aube.interact.model;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by huyaonan on 16/10/25.
 */
public class ReportResultModel extends BaseModel {
    public ReportResult data;

    public List<OptionItem> getOptions() {
        return data == null ? null : data.optionCountRespVoList;
    }

    public String getMessage() {
        if(data == null)
            return null;
        return data.message;
    }

    public List<GoodsItem> getGoodsList() {
        if(data == null)
            return null;
        return data.goodsList;
    }

    public static ReportResultModel parse(String result) {
        ReportResultModel response = new ReportResultModel();
        try {
            JSONObject jsonObject = new JSONObject(result);
            JSONObject data = jsonObject.optJSONObject("data");
            JSONArray optionArray = data.optJSONArray("optionCountRespVoList");
            JSONArray goodsArray = data.optJSONArray("goodsList");
            String message = data.optString("message");
            ReportResult reportResult = new ReportResult();
            reportResult.message = message;
            if(optionArray != null) {
                List<OptionItem> optionsItems = new ArrayList<>();
                for(int i=0; i<optionArray.length(); i++) {
                    JSONObject object = optionArray.getJSONObject(i);
                    optionsItems.add(OptionItem.parse(object));
                }
                reportResult.optionCountRespVoList = optionsItems;
            }
            if(goodsArray != null) {
                List<GoodsItem> goodsItems = new ArrayList<>();
                for(int i=0; i<goodsArray.length(); i++) {
                    JSONObject object = optionArray.getJSONObject(i);
                    goodsItems.add(GoodsItem.parse(object));
                }
                reportResult.goodsList = goodsItems;
            }
            response.data = reportResult;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return response;
    }

}